package com.xiaoying.server.publish.upload;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class UploadThreadManager implements Runnable {
	private static Log log = LogFactory.getFactory().getInstance(UploadThreadManager.class);
	
	
	private int maxThread;
	private LinkedBlockingQueue<Runnable> workQueue;
	private static Executor executor ;
	
	public UploadThreadManager(){
		maxThread = 10;
		workQueue = new LinkedBlockingQueue<Runnable>(maxThread);
		executor = Executors.newFixedThreadPool(maxThread);
	}
	
	private int getWorkQueueSize(){
		return this.workQueue.size();
	}
	
	/**
	 * 判断线程池有没有空闲线程
	 * @author ZJD
	 * @return
	 */
	public boolean isWorkQueueFull(){
		return this.getWorkQueueSize()>=maxThread?true:false;
	}
	
	public void submitTask(Runnable task){
		try {
			workQueue.put(task);
			System.out.println("workqueue Put方法");
		} catch (Exception e) {
			log.error(e);
		}
	}
	

	@Override
	public void run() {		
		while(true){			
			if(getWorkQueueSize()>0 && getWorkQueueSize()<=maxThread){
				try {
					Runnable task = workQueue.take();
					executor.execute(task);
				} catch (InterruptedException e) {
					log.error(e);
				}
			}			
			
		}
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	

}
